#include "Blockqueue.hpp"
#include "task.hpp"
#include <cstring>
#include<ctime>
#include<stdlib.h>
#include"RingQueue.hpp"
void *productorrun(void *data)
{
    //BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(data);
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(data);
    char arr[] = "+-*/%";
    while (1)
    {
        char way = arr[rand() % strlen(arr)];
        int x = rand() % 100;
        int y = rand() % 100;
        Task t(x, y, way);
        rq->push(t);
        // sleep(1);
        cout << t.to_productor() << endl;
    }
}
void *customerrun(void *data)
{
    //BlockQueue<Task> *bq = static_cast<BlockQueue<Task> *>(data);
    RingQueue<Task> *rq = static_cast<RingQueue<Task> *>(data);
    while (1)
    {
        sleep(3);
        Task t;
        rq->pop(t);
        t();
        cout<<t.to_customer()<<endl;
    }
}
int main()
{
    srand((uint64_t)time(0));
    //BlockQueue<Task> bq;
    RingQueue<Task> rq;
    pthread_t productor, customer;
    pthread_t productor1, customer1;
    pthread_t productor2, customer2;
    pthread_create(&productor,NULL,productorrun,&rq);
    pthread_create(&customer,NULL,customerrun,&rq);
    pthread_create(&productor1,NULL,productorrun,&rq);
    pthread_create(&customer1,NULL,customerrun,&rq);
    pthread_create(&productor2,NULL,productorrun,&rq);
    pthread_create(&customer2,NULL,customerrun,&rq);
    pthread_join(productor,nullptr);
    pthread_join(customer,nullptr);
    pthread_join(productor1,nullptr);
    pthread_join(customer1,nullptr);
    pthread_join(productor2,nullptr);
    pthread_join(customer2,nullptr);
    return 0;
}